package com.palfund.butterknife;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.palfund.butterknife.base.BaseActivity;

import java.util.List;

import butterknife.BindArray;
import butterknife.BindBitmap;
import butterknife.BindColor;
import butterknife.BindDrawable;
import butterknife.BindString;
import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnLongClick;

public class MainActivity extends BaseActivity {
    // 绑定view
    @BindView(R.id.btn1)
    Button mBtn1;
    @BindView(R.id.btn2)
    Button mBtn2;
    @BindView(R.id.btn3)
    Button mBtn3;
    //注意:的修饰类型不能是:private或者static.否则会报错:@BindView fields must not be private or static.
    // 绑定Views
    @BindViews({R.id.tv1, R.id.tv2, R.id.tv3})
    List<TextView> list_textView;
    // 绑定string
    @BindString(R.string.bindString)
    String bindString;
    // 绑定数组
    @BindArray(R.array.bindArray)
    String[] bindArray;
    @BindView(R.id.iv)
    ImageView mIv;
    // 绑定bitmap
    @BindBitmap(R.mipmap.ic_launcher)
    Bitmap mBitmap;
    // 绑定Color
    @BindColor(R.color.bindColor)
    int bindColor;
    // 绑定Drawable
    @BindDrawable(R.drawable.ic_launcher)
    Drawable mDrawable;

    @Override
    public void onCreate() {
        mBtn3.setText("btn3BindViewTest");
        mBtn2.setText(bindString);
        mBtn1.setText(bindArray[0]);
        mBtn1.setTextColor(bindColor);
        list_textView.get(0).setText("tv1BindViewsTest");
        mIv.setImageDrawable(mDrawable);
        BlankFragment blankFragment = new BlankFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.layoutCOntainer,
                blankFragment).commit();
        // Android中的Property属性也可以使用apply方法进行设置
        ButterKnife.apply(list_textView, View.ALPHA, 0.5f);
    }

    // View的点击
    @OnClick(R.id.btn1)
    public void onViewClicked() {
        Toast.makeText(this, "btnClick", Toast.LENGTH_SHORT).show();
    }

    @OnClick({R.id.tv2, R.id.tv3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv2:
                Toast.makeText(this, "tv2Clicks", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv3:
                Toast.makeText(this, "tv3Clicks", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @OnLongClick(R.id.tv1)
    public boolean onViewLongClick(View view) {
        Toast.makeText(this, "tv1OnLongClick", Toast.LENGTH_SHORT).show();
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, 1, 1, "ViewHolder   Bind");
        menu.add(Menu.NONE, 2, 1, "Apply");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 1:
                startActivity(new Intent(this, ViewHolderActivity.class));
                break;
            case 2:
                // 该函数一次性在列表中的所有View上执行一个动作
                ButterKnife.apply(list_textView, new ButterKnife.Action<TextView>() {
                    @Override
                    public void apply(@NonNull TextView view, int index) {
                        view.setEnabled(!view.isEnabled());
                    }
                });
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_main;
    }

}
